<template>
	<div class="app-container">
		<el-form :model="queryParams" ref="queryForm" :inline="true" label-width="60px">
			<el-form-item label="部门名" prop="deptName">
				<el-input v-model="queryParams.deptName" placeholder="请输入部门名" clearable size="small" @keyup.enter.native="handleQuery" />
			</el-form-item>

			<el-form-item label="状态" prop="withdrawalStatus">
				<el-select v-model="queryParams.withdrawalStatus" placeholder="选择审核" clearable size="small" prop="withdrawalStatus">
					<el-option v-for="dict in Options" :key="dict.dictValue" :label="dict.dictLabel" :value="dict.dictValue" />
				</el-select>
			</el-form-item>

			<!--            range-separator="至"-->
			<el-form-item label="时间">
				<div class="block">
					<el-date-picker v-model="dateRange" size="small" style="width: 240px" value-format="yyyy-MM-dd" type="daterange"
					 range-separator="-" start-placeholder="开始日期" end-placeholder="结束日期"></el-date-picker>
				</div>
			</el-form-item>

			<el-form-item>
				<el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
				<el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
			</el-form-item>
		</el-form>

		<!-- 提现tixianDialog -->
		<el-dialog title="提现" :visible.sync="tixianDialog" width="500px">
			<el-form ref="tixianForm" :model="tixianForm" label-width="100px">
				<el-form-item label="提现金额">
					<el-input v-model="tixianForm.money" placeholder="请输入金额"  />
				</el-form-item>

			</el-form>
			<div slot="footer" class="dialog-footer" style="margin-bottom: 30px">
				<el-button type="primary" @click="submitTixianForm('tixianForm')">确定</el-button>
				<!--  style="float: left;" -->
			</div>
		</el-dialog>

		<el-row :gutter="10" class="mb8">
			<el-col :span="1.5">
				<el-button type="primary" icon="el-icon-plus" size="mini" @click="handleAdd" v-hasPermi="['system:cash:add']">新增</el-button>
			</el-col>
			<!-- <el-col :span="1.5">
        <el-button
          type="success"
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['system:cash:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:cash:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['system:cash:export']"
        >导出</el-button>
      </el-col> -->
		</el-row>

		<el-table v-loading="loading" :data="cashList">
			<!-- <el-table-column type="selection" width="55" align="center" />
      @selection-change="handleSelectionChange"-->
			<!-- <el-table-column label="用户id" align="center" prop="cashId" /> -->
			<el-table-column label="部门名" align="center" prop="deptName" />
			<el-table-column label="银行卡号" align="center" prop="bankId" />
			<el-table-column label="实际到账金额" align="center" prop="factMoney" />
			<el-table-column label="申请提现金额" align="center" prop="money" />
			<el-table-column label="提现审核状态" align="center" prop="withdrawalStatus" :formatter="statusFormat1" />
			<el-table-column label="审核失败原因" align="center" prop="reason" />
			<el-table-column label="备注" align="center" prop="remark" />
			<el-table-column label="创建时间" align="center" prop="createTime" width="180">
				<template slot-scope="scope">
					<span>{{ parseTime(scope.row.createTime) }}</span>
				</template>
			</el-table-column>
			<el-table-column label="操作" align="center" class-name="small-padding fixed-width">
				<template slot-scope="scope">
					<el-button v-if="scope.row.withdrawalStatus=='0'" size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)"
					 v-hasPermi="['system:cash:edit']">修改</el-button>
				</template>
			</el-table-column>
		</el-table>

		<pagination v-show="total>0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
		 @pagination="getList" />

		<!-- 添加或修改公司申请提现对话框 -->
		<el-dialog :title="title" :visible.sync="open" width="500px">
			<el-form ref="submitForm" :model="form" label-width="100px" :rules="rules">
				<el-form-item label="银行卡号">
					<el-input v-model="form.bankId" placeholder="请输入银行卡id" disabled />
				</el-form-item>

				<el-form-item label="实际到账金额">
					<el-input v-model="form.factMoney" placeholder="请输入实际到账金额" disabled />
				</el-form-item>
				<el-form-item label="审核失败原因" prop="reason">
					<el-input v-model="form.reason" placeholder="请输入审核失败原因" type="textarea" :autosize="{ minRows: 2, maxRows: 4}" />
				</el-form-item>
			</el-form>
			<div slot="footer" class="dialog-footer" style="margin-bottom: 30px">
				<el-button style="float: left;" type="primary" @click="submitForm('submitForm')">审核成功</el-button>
				<el-button style="float: right;" @click="submitForm1('submitForm')">审核失败</el-button>
			</div>
		</el-dialog>
	</div>
</template>

<script>
	import {
		listCash,
		getCash,
		delCash,
		addCash,
		updateCash,
		exportCash
	} from "@/api/system/cash";

	export default {
		name: "Cash",
		data() {
			return {
				tixianForm: {},
				tixianDialog: false,
				// 时间筛选
				dateRange: [],
				Options: [],
				// 遮罩层
				loading: true,
				// 选中数组
				ids: [],
				// 非单个禁用
				single: true,
				// 非多个禁用
				multiple: true,
				// 总条数
				total: 0,
				// 公司申请提现表格数据
				cashList: [],
				// 弹出层标题
				title: "",
				// 是否显示弹出层
				open: false,
				// 查询参数
				queryParams: {
					pageNum: 1,
					pageSize: 10,
					deptName: undefined,
					createTime: undefined,
					// bankId: undefined,
					// userId: undefined,
					// factMoney: undefined,
					// money: undefined,
					withdrawalStatus: undefined
					// reason: undefined
					// createTime2: undefined
				},
				// 表单参数
				form: {},
				// 表单校验
				rules: {}
			};
		},
		created() {
			this.getDicts("sys_audit_status").then(response => {
				this.Options = response.data;
			});
			this.getList();
		},
		methods: {
			submitTixianForm(){
				console.log(this.tixianForm)
				if(!!this.tixianForm.money){
					addCash(this.tixianForm).then((response)=>{
						if (response.code === 200) {
							console.log("返回值", response);
							this.msgSuccess("操作成功");
							this.getList();
							this.tixianDialog=false
							this.reset();
						} else {
							this.msgError('系统内部错误!');
						}
					})
				}else{
					this.msgError('不可为空！');
				}
				
			},
			/** 提交按钮 */
			submitForm: function(formName) {
				this.form.withdrawalStatus = 1;
				this.$refs[formName].validate(valid => {
					// 0-待审核 1-审核成功 2-审核失败
					if (this.form.withdrawalStatus == "2") {
						//审核失败
						// alert('submit!');
						if (!!this.form.reason) {
							console.log("修改传的参数", this.form);
							updateCash(this.form).then(response => {
								if (response.code === 200) {
									console.log("修改成功返回值", response);
									this.msgSuccess("操作成功");
									this.form.withdrawalStatus = "";
									this.open = false;
									this.getList();
									this.reset();
								} else {
									this.msgError(response.msg);
								}
							});
						} else {
							this.msgError("申请不通过，失败原因不能为空！");
						}
					} else {
						console.log("修改传的参数", this.form);
						updateCash(this.form).then(response => {
							if (response.code === 200) {
								console.log("修改成功返回值", response);
								this.msgSuccess("操作成功");
								this.form.withdrawalStatus = "";
								this.open = false;
								this.getList();
								this.reset();
							} else {
								this.msgError(response.msg);
							}
						});
					}
				});
			},
			submitForm1: function(formName) {
				this.form.withdrawalStatus = 2;
				this.$refs[formName].validate(valid => {
					// 0-待审核 1-审核成功 2-审核失败
					if (this.form.withdrawalStatus == "2") {
						//审核失败
						// alert('submit!');
						if (!!this.form.reason) {
							console.log("修改传的参数", this.form);
							updateCash(this.form).then(response => {
								if (response.code === 200) {
									console.log("修改成功返回值", response);
									this.msgSuccess("操作成功");
									this.form.withdrawalStatus = "";
									this.open = false;
									this.getList();
									this.reset();
								} else {
									this.msgError(response.msg);
								}
							});
						} else {
							this.msgError("申请不通过，失败原因不能为空！");
						}
					} else {
						console.log("修改传的参数", this.form);
						updateCash(this.form).then(response => {
							if (response.code === 200) {
								console.log("修改成功返回值", response);
								this.msgSuccess("操作成功");
								this.form.withdrawalStatus = "";
								this.open = false;
								this.getList();
								this.reset();
							} else {
								this.msgError(response.msg);
							}
						});
					}
				});
			},
			statusFormat1(row, column) {
				return this.selectDictLabel(this.Options, row.withdrawalStatus);
			},
			/** 查询公司申请提现列表 */
			getList() {
				this.loading = true;
				listCash(this.addDateRange(this.queryParams, this.dateRange)).then(
					response => {
						this.cashList = response.rows;
						this.total = response.total;
						this.loading = false;
					}
				);
			},
			// 取消按钮
			cancel() {
				this.open = false;
				this.reset();
			},
			// 表单重置
			reset() {
				this.tixianForm = {
					money: undefined
				}
				this.form = {
					bankId: undefined,
					userId: undefined,
					factMoney: undefined,
					money: undefined,
					withdrawalStatus: undefined,
					reason: undefined,
					createTime: undefined,
					remark: undefined

					// cashId: undefined,
					// bankId: undefined,
					// userId: undefined,
					// factMoney: undefined,
					// money: undefined,
					// withdrawalStatus: "0",
					// reason: undefined,
					// createTime: undefined,
					// remark: undefined,
					// createBy: undefined,
					// createTime2: undefined,
					// updateBy: undefined,
					// updateTime: undefined
				};
				this.resetForm("form");
			},
			/** 搜索按钮操作 */
			handleQuery() {
				this.queryParams.pageNum = 1;
				this.getList();
			},
			/** 重置按钮操作 */
			resetQuery() {
				this.resetForm("queryForm");
				this.handleQuery();
			},
			// 多选框选中数据
			handleSelectionChange(selection) {
				this.ids = selection.map(item => item.cashId);
				this.single = selection.length != 1;
				this.multiple = !selection.length;
			},
			/** 新增按钮操作 */
			handleAdd() {
				this.reset();
				this.tixianDialog = true
				// this.open = true;
				// this.title = "添加公司申请提现";
			},
			/** 修改按钮操作 */
			handleUpdate(row) {
				this.reset();
				// const cashId = row.cashId || this.ids;
				// getCash(cashId).then(response => {
				//   this.form = response.data;
				//   this.open = true;
				//   this.title = "修改公司申请提现";
				// });

				// getCash(cashId).then(response => {
				// 	this.form = response.data;
				// 	this.open = true;
				// 	this.title = "修改公司申请提现";
				// });
				this.form = row;
				this.open = true;
				this.title = "修改公司申请提现";
			},
			/** 提交按钮 */
			// submitForm: function() {
			//   this.$refs["form"].validate(valid => {
			//     if (valid) {
			//       if (this.form.cashId != undefined) {
			//         updateCash(this.form).then(response => {
			//           if (response.code === 200) {
			//             this.msgSuccess("修改成功");
			//             this.open = false;
			//             this.getList();
			//           } else {
			//             this.msgError(response.msg);
			//           }
			//         });
			//       } else {
			//         addCash(this.form).then(response => {
			//           if (response.code === 200) {
			//             this.msgSuccess("新增成功");
			//             this.open = false;
			//             this.getList();
			//           } else {
			//             this.msgError(response.msg);
			//           }
			//         });
			//       }
			//     }
			//   });
			// },
			/** 删除按钮操作 */
			handleDelete(row) {
				const cashIds = row.cashId || this.ids;
				this.$confirm(
						'是否确认删除公司申请提现编号为"' + cashIds + '"的数据项?',
						"警告", {
							confirmButtonText: "确定",
							cancelButtonText: "取消",
							type: "warning"
						}
					)
					.then(function() {
						return delCash(cashIds);
					})
					.then(() => {
						this.getList();
						this.msgSuccess("删除成功");
					})
					.catch(function() {});
			},
			/** 导出按钮操作 */
			handleExport() {
				const queryParams = this.queryParams;
				this.$confirm("是否确认导出所有公司申请提现数据项?", "警告", {
						confirmButtonText: "确定",
						cancelButtonText: "取消",
						type: "warning"
					})
					.then(function() {
						return exportCash(queryParams);
					})
					.then(response => {
						this.download(response.msg);
					})
					.catch(function() {});
			}
		}
	};
</script>
